// 依赖类，每一个依赖对象都有一个依赖类
let currentEffect = null;
class Dep{
    // 初始化一个类时这个构造函数首先会被调用
    constructor(val){
        this._val = val;
        this.effects = new Set();
    }

    // 依赖收集
    depend(){
        console.log("in depend");
        if(currentEffect !== null){
            this.effects.add(currentEffect);
            console.log("add depend")
        }
    }

    // 依赖触发
    notice(){
        for(let item of this.effects){
            //console.log(item);
            item();
        }
    }

    get value(){
        this.depend();
        return this._val;
    }

    set value(newValue){
        this._val = newValue;
        this.notice();
    }

}
let a = 10;
let dep = new Dep(a);
function effectWatch(effect){
    currentEffect = effect;
    update();
    //dep.depend();
    this.currentEffect = null;
}

function update(){
    let b = dep.value + 10;
    console.log(b);
}

effectWatch(update);

dep.value = 20;
//dep.notice();